import time
import random

from PocketCube import PocketCube as Cube1
from FacePocketCubePuzzle import PocketCube as Cube2


def time_use_test(name: str, cube: type, formula: str):
    start_time = time.perf_counter()
    pocketCube = cube()
    pocketCube.act_formula(formula)
    ans = pocketCube.show_front_up_right_view()
    end_time = time.perf_counter()
    elapsed_time = end_time - start_time

    # print(f"{name} 用时 {elapsed_time:.6f} 秒")

    return elapsed_time


def analyze_records(records: dict):

    for config_name, time_uses in records.items():

        times = len(time_uses)

        min_time = min(time_uses)
        max_time = max(time_uses)
        time_uses.remove(min_time)
        time_uses.remove(max_time)

        avg_time = sum(time_uses) / times
        mid_time = sorted(time_uses)[int(times / 2)]

        print(f"{config_name} 测试次数: {times}  最小值: {min_time:.6f}  最大值: {max_time:.6f}  平均值: {avg_time:.6f}  中位数: {mid_time:.6f}")


if __name__ == '__main__':

    f = "UR'2U'L2D2LB'FL'RFB2U'F2DUFB2D'RFD'ULRD'L'R2FB'U'DF2U'D'R2FUF'D'L2FUDUR'2U'L2D2LB'FL'RFB2U'F2DUFB2D'RFD'ULRD'L'R2FB'U'DF2U'D'R2FUF'D'L2FUDUR'2U'L2D2LB'FL'RFB2U'F2DUFB2D'RFD'ULRD'L'R2FB'U'DF2U'D'R2FUF'D'L2FUD"

    configs = [
        ("CornerBased PocketCube", Cube1),
        ("FaceBased PocketCube", Cube2),
    ]

    records = {config[0]: [] for config in configs}

    for _ in range(1000):
        random_config = random.choice(configs)
        time_use = time_use_test(*random_config, f)

        records[random_config[0]].append(time_use)

    analyze_records(records)
